package bktech.soict.hust.bknote.activity.sms;

import java.util.ArrayList;
import java.util.List;

import bktech.soict.hust.bknote.R;
import bktech.soict.hust.bknote.activity.MainActivity;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class contact extends Activity {
	List<contactMembers> aContact = new ArrayList<contactMembers>();
	ArrayAdapter<contactMembers> adapter = null;
	ListView listContact;
	String name;
	String text;
	SmsHelper helper = MainActivity.helper;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contact);
		listContact = (ListView) findViewById(R.id.listContact);
		registerForContextMenu(listContact);
		adapter = new contactAdapter();
		listContact.setAdapter(adapter);
		Cursor cur = helper.queryContact();
		if (cur.moveToFirst()) {
			for (int i = 1; i <= cur.getCount(); i++) {
				contactMembers thongTin = new contactMembers();
				thongTin.setName(cur.getString(cur
						.getColumnIndexOrThrow("display_name")));
				thongTin.setPriority(cur.getString(cur.getColumnIndexOrThrow("priority")));
				adapter.add(thongTin);
				cur.moveToNext();
			}
			cur.close();
			listContact.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> arg0, View arg1,
						int position, long arg3) {
					// TODO Auto-generated method stub
					Cursor b = helper.queryContact();
					if (b.moveToFirst()) {
						for (int i = 1; i <= position; i++) {
							b.moveToNext();
						}
					}
					int contact_id = b.getInt(b
							.getColumnIndexOrThrow("contact_id"));
					Intent intent = new Intent(Intent.ACTION_VIEW, Uri
							.parse("content://contacts/people/" + contact_id));
					startActivity(intent);
				}

			});
		}
	}

	class contactAdapter extends ArrayAdapter<contactMembers> {
		TextView name;
		TextView txt;

		public contactAdapter() {
			super(contact.this, android.R.layout.simple_list_item_1, aContact);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;
			ContactHolder holder = null;
			if (row == null) {
				LayoutInflater inflater = getLayoutInflater();
				row = inflater.inflate(R.layout.row_contact, parent, false);
				holder = new ContactHolder(row);
				row.setTag(holder);
			} else {
				holder = (ContactHolder) row.getTag();
			}
			holder.populateFrom(aContact.get(position));
			return row;

		}

	}

	static class ContactHolder {
		TextView name;
		TextView priority;
		ImageView image;

		public ContactHolder(View row) {
			name = (TextView) row.findViewById(R.id.nameContact);
			priority = (TextView) row.findViewById(R.id.priority);
			image = (ImageView) row.findViewById(R.id.imageContact);

		}

		public void populateFrom(contactMembers r) {
			name.setText(r.getName());
			priority.setText(r.getPriority());
			image.setImageResource(R.drawable.contact);

		}

	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub

		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
				.getMenuInfo();
		int position = info.position;
		Cursor c = helper.queryContact();
		switch (item.getItemId()) {
		case R.id.nhom1:
			aContact.get(position).setPriority("1");
			adapter.notifyDataSetChanged();
			updateContact(c, position, 1);
			Toast.makeText(this, "Nhom 1", Toast.LENGTH_LONG).show();
			break;
		case R.id.nhom2:
			aContact.get(position).setPriority("2");
			adapter.notifyDataSetChanged();
			updateContact(c, position, 2);
			Toast.makeText(this, "Nhom 2", Toast.LENGTH_LONG).show();
			break;
		case R.id.nhom3:
			aContact.get(position).setPriority("3");
			adapter.notifyDataSetChanged();
			updateContact(c, position, 3);
			Toast.makeText(this, "Nhom 3", Toast.LENGTH_LONG).show();
			break;
		case R.id.nhom4:
			aContact.get(position).setPriority("4");
			adapter.notifyDataSetChanged();
			updateContact(c, position, 4);
			Toast.makeText(this, "Nhom 4", Toast.LENGTH_LONG).show();
			break;
		case R.id.nhom5:
			aContact.get(position).setPriority("5");
			adapter.notifyDataSetChanged();
			updateContact(c, position, 5);
			Toast.makeText(this, "Nhom 5", Toast.LENGTH_LONG).show();
			break;
		default:
			break;
		}
		c.close();
		return super.onContextItemSelected(item);
	}
	public void updateContact(Cursor c,int position,int priority){
		c = helper.queryContact();
		String number;
		String display_name;
		int contact_id;
		if(c.moveToFirst()){
			for(int i = 1; i <= position; i++ ){
				c.moveToNext();
			}
		}
		number = c.getString(c.getColumnIndexOrThrow("number"));
		display_name = c.getString(c.getColumnIndexOrThrow("display_name"));
		contact_id = c.getInt(c.getColumnIndexOrThrow("contact_id"));
		
		ContentValues cv = new ContentValues();
		cv.put("number", number);
		cv.put("display_name", display_name);
		cv.put("contact_id", contact_id);
		cv.put("priority", priority);
		
		SQLiteDatabase data = helper.getWritableDatabase();
		data.update("contact", cv, "contact_id = ?", new String[]{""+contact_id});
		
	}
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub

		super.onCreateContextMenu(menu, v, menuInfo);
		getMenuInflater().inflate(R.menu.context, menu);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

}